package com.tarry.trade.counter.util;


import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Random;

@Slf4j
public class Captcha {

    @Getter
    private String code;

    private BufferedImage image;

    private Random random = new Random();

    public Captcha(int width, int height, int codeCount, int lineCount) {
        // 生成图片
        image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // 背景色
        Graphics graphics = image.getGraphics();
        graphics.setColor(getRandColor(200, 250));
        graphics.fillRect(0, 0, width, height);
        Font font = new Font("Times New Roman", Font.PLAIN, height - 5);
        graphics.setFont(font);
        // 干扰线
        for (int i = 0; i < lineCount; i++) {
            int xs = random.nextInt(width);
            int ys = random.nextInt(height);
            int xe = xs + random.nextInt(width / 8);
            int ye = ys + random.nextInt(height / 8);
            graphics.setColor(getRandColor(160, 200));
            graphics.drawLine(xs, ys, xe, ye);
        }
        // 随机字符
        this.code = randomStr(codeCount);
        int fontWidth = width / codeCount;
        int fontHeight = height - 5;
        for (int i = 0; i < codeCount; i++) {
            String str = this.code.substring(i, i+ 1);
            graphics.setColor(this.getRandColor(1, 255));
            graphics.drawString(str, i * fontWidth + 3, fontHeight - 3);
        }
    }

    private String randomStr(int i) {
        char[] codeSequence = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
        int codeLen = codeSequence.length;
        String tempCode = "";
        for (int j = 0; j < i; j++) {
            tempCode = tempCode + codeSequence[random.nextInt(codeLen -1)];
        }
        return tempCode;
    }

    /**
     * 随机背景色
     *
     * @param fc
     * @param bc
     * @return
     */
    private Color getRandColor(int fc, int bc) {
        if (fc > 255) {
            fc = 255;
        }
        if (bc > 255) {
            bc = 255;
        }
        int r = fc + random.nextInt(bc - fc);
        int g = fc + random.nextInt(bc - fc);
        int b = fc + random.nextInt(bc - fc);
        return new Color(r, g, b);
    }

    public String getImageBase64Str() {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, "png", byteArrayOutputStream);
        } catch (IOException e) {
            log.error(e.getMessage());
            return null;
        }
        BASE64Encoder base64Encoder = new BASE64Encoder();
        String base64String = base64Encoder.encode(byteArrayOutputStream.toByteArray());
        base64String = base64String.replaceAll("\n", "").replaceAll("\r", "");
        return "data:image/jpg;base64," + base64String;
    }
}
